/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dpy.appmvdpy.Facades;

import com.dpy.appmvdpy.Models.Clientes;
import com.dpy.appmvdpy.Models.FormaPagamento;
import com.dpy.appmvdpy.Models.Produtos;
import com.dpy.appmvdpy.Models.Vendedor;
import java.util.List;
import javax.persistence.Query;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Lilli
 */
@Component
@Scope(value="request")
public class VendedorFacade extends DefaultFacade<Vendedor> {
   
    public VendedorFacade()
    {
       super(Vendedor.class);
    }
    
    @Override
    public void setId(Vendedor entity) {
          entity.setId(this.getMaxId()+1);
    }
    
    public int getMaxFaixa()
    {
      Query q = em.createQuery("SELECT max(inicioFaixaCadastro) from Vendedor as v");
      return q.getSingleResult() != null ? ((Number)q.getSingleResult()).intValue() : 0;
    }
    
    public Vendedor getByCodigoDPY(String codigo)
    {
      Query q = em.createQuery("SELECT v from Vendedor as v WHERE v.codigoDPY = :codigo");
      q.setParameter("codigo", codigo);
      return (Vendedor)q.getResultList().get(0);                   
    }
    
    public int proxNumFaixa(Vendedor vendedor)
    {          
        int inicioFaixa = vendedor.getInicioFaixaCadastro();
        
        Query q = em.createQuery("Select c from Clientes as c inner join c.vendedor as v where v.id = :id and cast(c.codigo as int) > :minId and cast(c.codigo as int) <= :maxId");
        q.setParameter("id", vendedor.getId());
        q.setParameter("minId", vendedor.getInicioFaixaCadastro());
        q.setParameter("maxId", vendedor.getFimFaixaCadastro());
        
        List<Clientes> clientes = (List<Clientes>)q.getResultList();
        
        int max = -1;
        if(clientes.size() > 0)
             max = Integer.valueOf(clientes.get(0).getCodigo());
        
        for(Clientes c: clientes)
        {
          if(max < Integer.valueOf(c.getCodigo()))
              max = Integer.valueOf(c.getCodigo());
        }        
        
        return max == -1 ? inicioFaixa : max; 
    }
    
    public Vendedor getVendedorByUser(String userId)
    {
       Query q = em.createQuery("select v from Vendedor as v inner join v.userId as u where u.username = :id");
       q.setParameter("id", userId);
       Vendedor v = (Vendedor)q.getResultList().get(0);
       return v;
    }
    
    @Transactional(readOnly=true)
    @Override
    public List<Vendedor> listAll()
    {
       Query q = em.createQuery("SELECT c from Vendedor as c ORDER BY c.nome ASC");
       return q.getResultList();
    }
    
       @Transactional(readOnly=true)
     @Override
    public List<Vendedor> listAll(int pag)
    {
       Query q = em.createQuery("SELECT c from Vendedor as c ORDER BY c.nome ASC");
       q.setFirstResult(pag * 100);
       q.setMaxResults(100);
       return q.getResultList();
    }
    
}
